package com.jeady.timetodo.activities.light

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableLongStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.jeady.timetodo.activities.common.CommonTaskUI
import com.jeady.timetodo.activities.common.TaskAction
import com.jeady.timetodo.activities.task.TaskType
import com.jeady.timetodo.base.TimeSlider
import com.jeady.timetodo.services.updateRunningService
import com.jeady.timetodo.ui.theme.TimeToDoTheme


class LightControlActivity: ComponentActivity() {
    private val TAG = "[LightControlActivity]"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            CommonTaskUI(
                "定时开关手机手电筒",
                "请避免长时间开启手电筒，一切后果自行承担。",
                listOf(
                    TaskAction("开启手电筒", TaskType.LIGHT_ON),
                    TaskAction("关闭手电筒", TaskType.LIGHT_OFF)
                )
            ){}
//            LightControlUI()
        }
    }
    @Composable
    private fun LightControlUI() {
        TimeToDoTheme {
            Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center){
                Text("定时开关手电筒",
                    Modifier.align(Alignment.TopCenter).padding(top = 50.dp),
                    fontSize = 30.sp,
                    fontWeight = FontWeight.ExtraBold
                )
                Surface(shadowElevation = 3.dp){
                    Column(
                        Modifier
                            .fillMaxWidth(0.9f)
                            .padding(10.dp)
                    ) {
                        var timeSetting by remember { mutableLongStateOf(0) }
                        TimeSlider {
                            timeSetting = it
                        }
                        Row(
                            Modifier.fillMaxWidth(),
                            horizontalArrangement = Arrangement.SpaceAround
                        ) {
                            Button(onClick = {
                                updateRunningService(baseContext, TaskType.LIGHT_ON, timeSetting)
                            }) {
                                Text("${timeSetting/1000}秒后\n开灯", textAlign = TextAlign.Center)
                            }
                            Button(onClick = {
                                updateRunningService(baseContext, TaskType.LIGHT_OFF, timeSetting)
                            }) {
                                Text("${timeSetting/1000}秒后\n关灯", textAlign = TextAlign.Center)
                            }
                        }
                    }
                }
            }
        }
    }
}

